<?php
class TablesController extends AppController {

	var $name = 'Tables';

	function index() {
		$this->Table->recursive = 0;
		$this->set('tables', $this->paginate());
	}

	function admin_index() {
		$this->Table->recursive = 0;
		$this->set('tables', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid table', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('table', $this->Table->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Table->create();
			if ($this->Table->save($this->data)) {
				$this->Session->setFlash(__('The table has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The table could not be saved. Please, try again.', true));
			}
		}
		$areas = $this->Table->Area->find('list');
		$this->set(compact('areas'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid table', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Table->save($this->data)) {
				$this->Session->setFlash(__('The table has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The table could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Table->read(null, $id);
		}
		$areas = $this->Table->Area->find('list');
		$this->set(compact('areas'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for table', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Table->delete($id)) {
			$this->Session->setFlash(__('Table deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Table was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	
	//------------------------------------------

	// retrieve order from mobile
	function admin_retrieve(){
		$id = $this->params['pass'][2];
		$this->layout=false;
		if(!isset($id) || !is_numeric($id)){
			return 'null';
		}
		$this->loadModel('OrderItem');
		$this->OrderItem->recursive = 1;
		// tìm order đang ở trạng thái chưa trả và có table_id = $id
		$conditions = array('Order.table_id'=>$id,'Order.paid'=>'0');
		$OrderItems = $this->OrderItem->find('all',array('conditions'=>$conditions));
		// nếu bàn này hiện tại chưa có order nào đang trong trạng thái chưa thanh toán
		if(count($OrderItems)==0){
			$this->autoRender = false;
			return 'null';
		}
		$this->set('OrderItems',$OrderItems);
		// lấy thông tin nhân viên
		$this->loadModel('Employee');
		$this->Employee->recursive = 0;
		$conditions = array('Employee.id'=>$OrderItems[0]['Order']['employee_id']);
		$this->set('employee',$this->Employee->find('first',array('conditions'=>$conditions)));
	}
	
	// lấy trạng thái của bàn
	function admin_getinfor(){
		$tableId = $this->params['pass'][2];
		$this->layout = false;
		if(!$tableId){
			return;
		}
		else{
			$this->Table->recursive = 0;
			// lấy thông tin sử dụng của bàn
			$tableInfor = $this->Table->find('first',array('conditions'=>array('Table.id'=>$tableId)));
			$this->loadModel('Order');
			$tableInfor['Table']['ordered'] = $this->Order->isOdered($tableId);
			$tableInfor['Table']['prepared'] = $this->Order->isOrderingPrepared($tableId);
			$this->set('tableInfor',$tableInfor['Table']);
		}
	}
}
?>